# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: jaewon

from enum import IntEnum
from collections import namedtuple
from recordclass import recordclass


# TODO: TradeSide = coin.proto.coin_market_enums_pb2.TradeSide
# Do not confused with OrderSide.
# OrderSide is used in your order for Order Submit, Cancel, and Fill.
# TradeSide is used in market for Trade.
class TradeSide(IntEnum):
  TRADE_NO_SIDE = 0
  TRADE_BUY_SIDE = 1
  TRADE_SELL_SIDE = 2
  TRADE_SELL_SIDE_DEPRECATED = -1  # for old data


MarketTradeEvent = namedtuple(
    'MarketTradeEvent',
    ['timestamp', 'symbol', 'price', 'qty', 'side', 'has_more_trade', 'topic_id'])


def create_market_trade_event(timestamp,
                              symbol,
                              price,
                              qty,
                              side,
                              has_more_trade=False,
                              *,
                              topic_id=None):
  return MarketTradeEvent(timestamp=timestamp,
                          symbol=symbol,
                          price=price,
                          qty=qty,
                          side=side,
                          has_more_trade=has_more_trade,
                          topic_id=topic_id)


MarketTickerEvent = recordclass('MarketTickerEvent',
                                [
                                    'timestamp',
                                    'product',
                                    'period',
                                    'open',
                                    'close',
                                    'low',
                                    'high',
                                    'trade_amount',
                                    'vol_24h',
                                    'open_interest',
                                    'limit_low',
                                    'limit_high'
                                ])


def create_market_ticker_event(timestamp,
                               product,
                               period=None,
                               open=None,
                               close=None,
                               low=None,
                               high=None,
                               trade_amount=None,
                               vol_24h=None,
                               open_interest=None,
                               limit_low=None,
                               limit_high=None):
  return MarketTickerEvent(timestamp,
                           product,
                           period,
                           open,
                           close,
                           low,
                           high,
                           trade_amount,
                           vol_24h,
                           open_interest,
                           limit_low,
                           limit_high)


MarketIndexEvent = recordclass('MarketIndexEvent', ['timestamp', 'product', 'index'])


def create_market_index_event(timestamp, product, index):
  return MarketIndexEvent(timestamp, product, index)


MarketInstrumentEvent = recordclass(
    'MarketInstrumentEvent',
    ['timestamp', 'product', 'funding_rate', 'mark_price', 'open_interest', 'topic_id'])


def create_market_instrument_event(timestamp,
                                   product,
                                   *,
                                   funding_rate=None,
                                   mark_price=None,
                                   open_interest=None,
                                   topic_id=None):
  return MarketInstrumentEvent(timestamp=timestamp,
                               product=product,
                               funding_rate=funding_rate,
                               mark_price=mark_price,
                               open_interest=open_interest,
                               topic_id=topic_id)
